<template>
	<div class="page index" @click="hideLeadImg">
		<div class="headbar"></div>
		<x-header 
			class="title"
			:left-options="{backText: '', preventGoBack: true}"
			@on-click-back="backToApp">发票查验
			<a v-if="!callCamera" class="icon icon-scan" slot="right" @click.prevent="selectMethod">
			</a><a v-if="callCamera" class="icon icon-hand" slot="right" @click.prevent="selectMethod"></a>
		</x-header>
		<group class="content-cell" labelWidth="110px" >
			<x-input
				ref="invoiceCode"
				class="invoice-code fz-15"
				type="tel"
				title="发票代码"
				v-model="invoiceCode"
				@on-blur="onBlur('invoiceCode')"
				@on-change="onChange('invoiceCode')"
				:max="12"
				placeholder="请输入发票代码（必填）"></x-input>
			<x-input
				ref="invoiceNum"
			 	class="fz-15"
				type="tel"
				title="发票号码"
				v-model="invoiceNum"
				@on-blur="onBlur('invoiceNum')"
				@on-change="onChange('invoiceNum')"
				:max="8"
				placeholder="请输入发票号（必填）"></x-input>
			
			<!-- 库里若不存在，填写额外发票信息 -->
			<div class="flex-title-wrap fz-15" v-show="hasData" @click="selectDate">
				<div class="flex-label">
					<label>开票日期</label>
				</div>
				<div class="flex-value">
					<input
						ref="invoiceDate"
						class="calender-input"
						type="text"
						@focus="onFocus('invoiceDate')"
						v-model.trim="invoiceDate"
						placeholder="请选择（必填）" readonly>
				</div>
			</div>
			<!-- 专票 -->
			<x-input
				ref="hasNoTaxAmountOne"
				v-show="hasNoTaxAmountOne"
				class="fz-15"
				type="text"
				title="金额（不含税）"
				v-model="noTaxAmount"
				@on-change="onChange('noTaxAmountOne')"
				:max="11"
				placeholder="请输入开具不含税金额（必填）"></x-input>
			<!-- 机动车销售统一发票 -->
			<x-input
				ref="hasNoTaxAmountTwo"
				v-show="hasNoTaxAmountTwo"
				class="fz-15"
				type="text"
				title="不含税价"
				v-model="noTaxAmount"
				@on-change="onChange('noTaxAmountTwo')"
				:max="11"
				placeholder="请输入不含税价（必填）"></x-input>
			<!-- 普通发票、电子发票、卷票 -->
			<x-input
				ref="checkCode"
				v-show="hasCheckCode"
				class="fz-15"
				type="tel"
				title="校验码"
				v-model="checkCode"
				@on-change="onChange('checkCode')"
				:max="6"
				placeholder="请输入校验码后六位（必填）"></x-input>
		</group>
		<p class="tips fz-13">注：当日开具的发票最快可于次日进行查验；支持查验最近一年内开具的发票</p>
		<box gap="10px 10px">
			<x-button class="btn-check" v-bind:class="{'weui-btn_disabled': !nextBtn}" @click.native.prevent="check">查验</x-button>
			<x-button class="btn-reset" v-bind:class="{'weui-btn_disabled': !resetBtn}" @click.native.prevent="reset">重置</x-button>
		</box>

		<div v-transfer-dom>
			<popup v-model="datePickerShow" position="bottom">
				<div class="popup-tab">
					<a @click="dataPickClosed">关闭</a>
					<h3>选择时间</h3>
					<a @click="dataPickSure">确定</a>
				</div>
				<div class="calender-wrap" ref="calender">
					<div v-if="clickCalendar">
						<div class="calender-list" v-for="(date, index) in dateList" :key="index">
							<div class="calender-header">{{date.year + '年' + date.month}} 月</div>
							<inline-calendar
								ref="calendar"
								v-model="value"
								:render-month="[date.year, date.month]"
								:start-date="date.startDate"
								:end-date="date.endDate"
								:show-last-month="false"
								:show-next-month="false"
								:render-on-value-change="false"
								:highlight-weekend="true"
								:hide-header="true"
								:replace-text-list="replaceTextList"
								:disable-future="true">
							</inline-calendar>
						</div>
					</div>
				</div>
			</popup>
		</div>
		<!-- <input ref="title" type="text" placeholder="测试聚焦的"> -->
		<div class="lead-shadow" v-if="lead">
			<!-- <img class="lead-img-1" src="../assets/images/lead-1.png" alt="lead-1">
			<img class="lead-img-2" src="../assets/images/lead-2.png" alt="lead-2">
			<img class="lead-img-3" src="../assets/images/lead-3.png" alt="lead-3"> -->
			<img src="../assets/images/lead.png" alt="">
		</div>
	</div>
</template>

<script>
import {TransferDom, Popup, XSwitch, XHeader, Group, XInput, Box, XButton, Datetime, InlineCalendar} from 'vux';
import axios from 'axios';
import $ from 'jquery'
import CryptoJS  from 'crypto-js';
import store from 'store';
import {ajaxDomain, routerDomain} from '../config/config';
import qs from 'qs'


const Index = {
	name: 'index',
	directives: {
		TransferDom
	},
	components: {
		Popup, XSwitch, XHeader, Group, XInput, Box, XButton, Datetime, InlineCalendar
	},
	data () {
		return {
			// 是否显示引导页
			lead: true,
			// 判断是否可以点击查验
			nextBtn: false,
			// 判断是否可以重置
			resetBtn: false,
			// 记录是手动输入还是调取手机摄像头扫描二维码
			callCamera: false,
			// 

			// 是否库里有数据
			hasData: false,
			hasNoTaxAmountOne: false,
			hasNoTaxAmountTwo: false,
			hasCheckCode: false,
			invoiceCode: '',
			invoiceNum: '',
			invoiceDate: '',
			noTaxAmount: '',
			checkCode: '',

			replaceTextList: {
				'TODAY':'今天'
			},
			value: '',
			// 点击使用的时候才渲染，减少移动端输入时卡顿严重
			clickCalendar: false,
			datePickerShow: false,
			dateList: []
		}
	},
	created () {

		// 调用相机扫描二维码后调用函数返回data
		window.scanResult = (data) => {
			// alert(data)
			
			this.fillDate(data);
		}

		// store.remove('callCamera')

		// 判断上一次打开是扫描的还是手动输入的页面
		if (store.get('callCamera') == 1) {
			// this.callCamera = true;
			// 调用android，IOS方法
			window.nuonuoAPP && window.nuonuoAPP.scanQRCode();
		}
		
		// 初始化日历
		this.calendarInit();

		// 清除缓存
		if (store.get('invoice')) {
			store.remove('invoice');
		}
		
		if (localStorage.getItem('lead') == 1) {
			this.lead = false;
		}
		
	},
	mounted () {

		this.$refs.invoiceCode.$refs.input.addEventListener('keyup', (e) => {
			this.invoiceCode = e.target.value.replace(/[^0-9]/g,'');
		});

		this.$refs.invoiceNum.$refs.input.addEventListener('keyup', (e) => {
			this.invoiceNum = e.target.value.replace(/[^0-9]/g,'');
		});

		
		this.$refs.checkCode.$refs.input.addEventListener('keyup', (e) => {
			this.checkCode = e.target.value.replace(/[^0-9]/g,'');
		});
		
		
		this.$refs.hasNoTaxAmountOne.$refs.input.addEventListener('keyup', (e) => {
			// this.noTaxAmount = e.target.value.replace(/[^\d^\.]+/g,'');

			this.noTaxAmount = this.noTaxAmount.replace(/[^\d^\.]/g,""); //清除"数字"和"."以外的字符  
			this.noTaxAmount = this.noTaxAmount.replace(/^\./g,""); //验证第一个字符是数字而不是  
			this.noTaxAmount = this.noTaxAmount.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的  
			this.noTaxAmount = this.noTaxAmount.replace(".","$#$").replace(/\./g,"").replace("$#$",".");  
			this.noTaxAmount = this.noTaxAmount.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
		});

		this.$refs.hasNoTaxAmountTwo.$refs.input.addEventListener('keyup', (e) => {
			this.noTaxAmount = this.noTaxAmount.replace(/[^\d^\.]/g,""); //清除"数字"和"."以外的字符  
			this.noTaxAmount = this.noTaxAmount.replace(/^\./g,""); //验证第一个字符是数字而不是  
			this.noTaxAmount = this.noTaxAmount.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的  
			this.noTaxAmount = this.noTaxAmount.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
			this.noTaxAmount = this.noTaxAmount.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
		});
		
		
		// $('.invoice-code input').trigger('click').focus();
		
		
	},
	methods: {
		focusInvoiceCode () {
			this.$refs.invoiceCode.focus();
		},
		// --method--
		// 导航页
		hideLeadImg () {
			if (this.lead) {
				this.lead = false;
				localStorage.setItem('lead', 1);
			}
		},
		// --method--
		// 退出发票查验，返回诺言APP
		backToApp () {
			if (!this.lead) {
				window.nuonuoAPP && window.nuonuoAPP.quit()
			}
		},
		// --method--
		// 右上角切换查验方式  （二维码扫描和手动输入）
		selectMethod () {
			// console.log(!this.lead && !this.callCamera)
			if (!this.lead && !this.callCamera) {
				// console.log('调用摄像头')
				// 调用摄像头
				// this.callCamera = true;
				store.set('callCamera', 1);
				// 调用android，IOS方法
				window.nuonuoAPP && window.nuonuoAPP.scanQRCode();
			} else {
				// // 手动输入信息
				// this.callCamera = false;
				// store.set('callCamera', 2);
			}
			
		},
		// --method--
		// 初始化日历
		calendarInit () {
			var date = new Date();
			var year = date.getFullYear();
			var month = date.getMonth() + 1;
			var monthCopy = month;
			var day = date.getDate();
			var dayCopy = day;
			var lastYear = true;
			var count;
			var tempArr = [];
			var obj = {};
			
			// 如果是每个月的1号，只循环12次，否则循环13次
			if (day === 1) {
				count = 12;
			} else {
				count = 13;
			}
			// 如果是1月1号，起始日期和终止日期均在前一年
			// if (month === 1 && day === 1) {
			// 	year --;
			// }
			// 根据月份推算前一个月份是31天还是30天还是29天（二月）
			if (day === 1) {
				if (monthCopy > 1 && monthCopy - 1 === 1 || monthCopy - 1 === 3 || monthCopy - 1 === 5 || monthCopy - 1 === 7 || monthCopy - 1 === 8 || monthCopy - 1 === 10 || monthCopy - 1 === 12) {
					dayCopy = 31;
				} else if (monthCopy - 1 === 4 || monthCopy - 1 === 6 || monthCopy - 1 === 9 || monthCopy - 1 === 11) {
					dayCopy = 30;
				} else if (monthCopy - 1 === 2 && (year%4 == 0)) {
					dayCopy = 29;
				} else if (monthCopy - 1 === 2 && (year%4 != 0)) {
					dayCopy = 28;
				}
			} else {
				dayCopy --;
			}
			
			// console.log(year + '-' + (monthCopy-1) + '-' + dayCopy)
			for (let i = 0; i < count; i ++) {
				if (month <= 12 && lastYear) {
						obj = {
						year: year - 1,
						month: month,
						day: day,
						startDate: (year - 1) + '-' + monthCopy + '-' + day,
						endDate: year + '-' + monthCopy + '-' + dayCopy
					}
					

					// tempArr.push({
					// 	year: year - 1,
					// 	month: month,
					// 	day: day,
					// 	startDate: (year - 1) + '-' + monthCopy + '-' + day,
					// 	endDate: year + '-' + monthCopy + '-' + dayCopy
					// });
					// month ++;
				} else {
						obj = {
						year: year,
						month: month,
						day: day,
						startDate: (year - 1) + '-' + monthCopy + '-' + day,
						endDate: year + '-' + monthCopy + '-' + dayCopy
					}
					// tempArr.push(obj)

					// tempArr.push({
					// 	year: year,
					// 	month: month,
					// 	day: day,
					// 	startDate: (year - 1) + '-' + monthCopy + '-' + day,
					// 	endDate: year + '-' + monthCopy + '-' + dayCopy
					// });
					// month ++;
				}
				month ++;
				tempArr.push(obj)
					
				if (month > 12 && lastYear) {
					lastYear = false;
					month = 1;
				}


				// this.dateList.push({
				// 	year: 2017,
				// 	month: 10,
				// 	day: 17,
				// 	startDate: '2016-10-17',
				// 	endDate: '2017-10-16'
				// })
				// this.dateList = [1,2,3,4,5,6,7,8,9,10,11,12]
			}

			this.dateList = tempArr;
		},
		keyup (val) {
			console.log(val)
		},
		onFocus (val) {
			if (val === 'invoiceDate') {
				this.$refs.invoiceDate.blur();
			}
		},
		onBlur (val) {
			const DMLength = this.invoiceCode.length,
				  HMLength = this.invoiceNum.length;
				  
			if (val === 'invoiceCode') {
				if (DMLength != 10 && DMLength != 12) {
					this.$vux.toast.show({
						type: 'text',
						width: '238px',
						text: '请输入正确的发票代码'
					});
					return false;
				}
			} else if (val === 'invoiceNum') {
				if (HMLength !== 8) {
					this.$vux.toast.show({
						type: 'text',
						width: '238px',
						text: '请输入正确的发票号码'
					});
					return false;
				}
			}

		},
		onChange (val) {
			// 实时判断输入的值，查验按钮是否可用
			const DMLength = this.invoiceCode.length,
				  HMLength = this.invoiceNum.length,
				  flag = this.noTaxAmount;
			
			// if (val == 'noTaxAmountOne' || val == 'noTaxAmountTwo') {
				// if (flag.charAt(flag.length - 1) == '.') {
				// 	// console.log(this.$refs.hasNoTaxAmountOne)
				// 	this.$refs.hasNoTaxAmountOne.max = 2;
				// 	this.$refs.hasNoTaxAmountTwo.max = 2;
				// }
			// }

			/* 校验，当已经查验过，删除发票代码至第八位或者清除代码时收起日期选择和金额、校验码 */
			if (this.hasData && DMLength === 8) {
				// 收起日期选择和校验码或金额
				this.hasData = false;
				this.hasNoTaxAmountOne = false;
				this.hasNoTaxAmountTwo = false;
				this.hasCheckCode = false;
			}
			if (this.hasData && DMLength === 0) {
				// 收起日期选择和校验码或金额
				this.hasData = false;
				this.hasNoTaxAmountOne = false;
				this.hasNoTaxAmountTwo = false;
				this.hasCheckCode = false;
			}
			/* 校验，当已经查验过，删除发票代码至第八位或者清除代码时收起日期选择和金额、校验码 */

			// 查验按钮是否置灰
			if (DMLength >= 10 && DMLength <= 12 && HMLength >= 8 && HMLength <= 12) {
				this.nextBtn = true;
			} else {
				this.nextBtn = false;
			}

			// 重置按钮是否置灰
			if (DMLength || HMLength) {
				this.resetBtn = true;
			} else {
				this.resetBtn = false;
			}
		},
		// --method--
		// 将二维码扫描的数据填充至表单
		fillDate (data) {
			// 解析二维码扫取后得到的字符串
			var dataArr = data.split(',');
			if (dataArr.length > 3) {
				this.invoiceCode = dataArr[2];
				this.invoiceNum = dataArr[3];
				this.noTaxAmount = dataArr[4];
				this.invoiceDate = dataArr[5].slice(0, 4) + '-' + dataArr[5].slice(4, 6) + '-' + dataArr[5].slice(6, 8);
				this.checkCode = dataArr[6].slice(-6);

				var invoiceType =  this.checkInvoiceType(this.invoiceCode);
				if (invoiceType === 'TYPE_ERROR') {
					this.$vux.toast.show({
						type: 'text',
						width: '238px',
						text: '发票类型错误'
					});
					return false;
				} else if (invoiceType === 'TYPE_SPECIAL') {
					this.hasNoTaxAmountOne = true;
				} else if (invoiceType === 'TYPE_COMMON' || invoiceType === 'TYPE_NET_COMMON' || invoiceType === 'TYPE_ROLLUP') {
					this.hasCheckCode = true;
				} else if (invoiceType === 'TYPE_VEHICLE') {
					this.hasNoTaxAmountTwo = true;
				}
				
				this.hasData = true;
				this.nextBtn = true;
				this.QRcodeCheck();
			} else {
				this.$vux.toast.show({
					type: 'text',
					width: '238px',
					text: '请手动输入信息'
				});
			}
		},
		// --method--
		// 点击开票日期表单，跳出日历框
		selectDate () {
			this.clickCalendar = true;
			this.datePickerShow = true;

			// console.log('clientHeight', $('.calender-wrap'))
			this.$nextTick(() => {
				// console.log('this.$refs.calender', this.$refs.calender.scrollTop)
				// console.log('this.$refs.calender.scrollHeight', this.$refs.calender.scrollHeight)

				// 将日期滚动至最底部
				setTimeout(() => {
					window.scrollTo(0, this.$refs.calender.scrollHeight - 10)
				}, 300)
				
			})
			
			
		},
		// --method--
		// 点击关闭日历弹出框
		dataPickClosed () {
			this.datePickerShow = false;
			this.clickCalendar = false;
		},
		// --method--
		// 点击确定开票日期
		dataPickSure () {
			this.invoiceDate = this.value;
			this.datePickerShow = false;
			this.clickCalendar = false;

			if (this.hasNoTaxAmountTwo) {
				this.$refs.hasNoTaxAmountTwo.focus();
			} else if (this.hasNoTaxAmountOne) {
				this.$refs.hasNoTaxAmountOne.focus();
			} else if (this.hasCheckCode) {
				this.$refs.checkCode.focus();
			}

			// 将日期滚动至顶部，防止内容被顶上去
			window.scrollTo(0, 0)
		},
		// --method--
		// 根据发票代码判断发票类型
		checkInvoiceType (invoiceCode) {
			var TYPE_COMMON = 'TYPE_COMMON';			//增值税普通发票!
			var TYPE_SPECIAL = 'TYPE_SPECIAL';			//增值税专用发票!
			var TYPE_NET_COMMON = 'TYPE_NET_COMMON';	//增值税电子普通发票!
			var TYPE_ROLLUP = 'TYPE_ROLLUP';			//增值税普通发票（卷票）!
			var TYPE_VEHICLE = 'TYPE_VEHICLE';			//机动车销售统一发票!
			var TYPE_ERROR = 'TYPE_ERROR';				//发票类型错误
			var TYPE_PASS = 'TYPE_PASS';				//通行证发票
    		var TYPE_USED_CAR = 'TYPE_USED_CAR';		//通行证发票



			// 判断发票类型，根据头部发票代码判断
			var special=["144031539110","131001570151","133011501118","111001571071"];
			var flag = "";
			if (invoiceCode.length == 12) {
				flag = invoiceCode.substring(10,12);
				for (var i = 0; i < special.length; i++) {
					if (invoiceCode == special[i]) {
						return TYPE_NET_COMMON;
					}
				}
				//判断是否为新版电子票
				if (invoiceCode.charAt(0) == "0" && flag == "11") {
					return TYPE_NET_COMMON;
				}
				if (invoiceCode.charAt(0) == "0" && (flag == "06" || flag == "07")) {
					//判断是否为卷式发票  第1位为0且第11-12位为06或07
					return TYPE_ROLLUP;
				}
				if (invoiceCode.charAt(0) != "0" && invoiceCode.charAt(7) == "2") {
					return TYPE_VEHICLE;
				}
				if (invoiceCode.charAt(0) == "0" && (flag == "04" || flag == "05")) {
					//新增12位普票
					return TYPE_COMMON;
				}
				if (invoiceCode.charAt(0) == "0" && (flag == "12")) {
					//新增12位通行证发票
					return TYPE_PASS;
				}
				if (invoiceCode.charAt(0) == "0" && (flag == "17")) {
					//新增12位二手车发票
					return TYPE_USED_CAR;
				}

			}
			if (invoiceCode.length == 10) {
				if (invoiceCode.charAt(7) == "1" || invoiceCode.charAt(7) == "5") {
					return TYPE_SPECIAL;
				} else if (invoiceCode.charAt(7) == "6" || invoiceCode.charAt(7) == "3") {
					return TYPE_COMMON;
				}
			}
			return TYPE_ERROR;
		},
		// --method--
		// AES加密
		AESencrypt (plaintText) {
			// AES加密，用于ajax参数上传
			// key 秘钥
			// return 加密后数据
			var key = CryptoJS.enc.Utf8.parse("zjaisino_zjjrpta");
			var plaintText = JSON.stringify(plaintText)
			var encryptedData = CryptoJS.AES.encrypt(plaintText, key, {
				mode: CryptoJS.mode.ECB,
				padding: CryptoJS.pad.Pkcs7
			});

			encryptedData = encryptedData.ciphertext.toString();
		
			var encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedData);
			var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);

			return  encryptedBase64Str;
		},
		// --method--
		// AES解密
		AESdecrypt (encryptedBase64Str) {
			// AES解密，用于解析服务端返回的data数据
			// key 秘钥
			// return 解密后数据
			var key = CryptoJS.enc.Utf8.parse("zjaisino_zjjrpta");
			var decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
				mode: CryptoJS.mode.ECB,
				padding: CryptoJS.pad.Pkcs7
			});
			
			var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
			
			return JSON.parse(decryptedStr);
		},
		// --method--
		// 校验输入的值
		validation () {
			var invoiceCode = this.invoiceCode,
				invoiceNum = this.invoiceNum,
				invoiceDate = this.invoiceDate,
				noTaxAmount = this.noTaxAmount,
				checkCode = this.checkCode,
				noTaxAmount = this.noTaxAmount;
			
			if (invoiceCode.length != 10 && invoiceCode.length != 12) {
				this.$vux.toast.show({
					type: 'text',
					width: '238px',
					text: '请输入正确的发票代码'
				});
				return 0;
			} else if (invoiceNum.length !== 8) {
				this.$vux.toast.show({
					type: 'text',
					width: '238px',
					text: '请输入正确的发票号码'
				});
				return 0;
			} else if (this.hasData) {
				// 国税数据库查询，验证增加几个字段
				if (!invoiceDate) {
					this.$vux.toast.show({
						type: 'text',
						width: '238px',
						text: '请选择开票日期'
					});
					return 0;
				} else if (this.hasCheckCode) {
					if (checkCode.length !== 6) {
						this.$vux.toast.show({
							type: 'text',
							width: '238px',
							text: '校验码为后六位'
						});
						return 0;
					}
				} else if (this.hasNoTaxAmountOne) {
					if (!noTaxAmount) {
						this.$vux.toast.show({
							type: 'text',
							width: '238px',
							text: '请输入开具不含税金额'
						});
						return 0;
					} else if (parseFloat(noTaxAmount) > 99999999.99) {
						this.$vux.toast.show({
							type: 'text',
							width: '238px',
							text: '开具不含税金额最大为99999999.99'
						});
						return 0;
					} else if (!/^[0-9]+$|^[0-9]+\.[0-9]{1,10}$/.test(noTaxAmount)) {
						this.$vux.toast.show({
							type: 'text',
							width: '238px',
							text: '请输入正确开具不含税金额'
						});
						return 0;
					}
				} else if (this.hasNoTaxAmountTwo) {
					if (!noTaxAmount) {
						this.$vux.toast.show({
							type: 'text',
							width: '238px',
							text: '请输入不含税价'
						});
						return 0;
					} else if (parseFloat(noTaxAmount) > 99999999.99) {
						this.$vux.toast.show({
							type: 'text',
							width: '238px',
							text: '不含税价金额最大为99999999.99'
						});
						return 0;
					} else if (!/^[0-9]+$|^[0-9]+\.[0-9]{1,10}$/.test(noTaxAmount)) {
						this.$vux.toast.show({
							type: 'text',
							width: '238px',
							text: '请输入正确不含税价金额'
						});
						return 0;
					}
				}
			}
			return 1;
		},
		// --method--
		// 重置输入
		reset () {
			if (this.resetBtn) {
				// 清空所有输入
				this.invoiceCode = '';
				this.invoiceNum = '';
				this.invoiceDate = '';
				this.noTaxAmount = '';
				this.checkCode = '';

				// 收起日期选择和校验码或金额
				this.hasData = false;
				this.hasNoTaxAmountOne = false;
				this.hasNoTaxAmountTwo = false;
				this.hasCheckCode = false;
			}
		},
		// --method--
		// 二维码扫描查验
		QRcodeCheck () {
			var url;
			// 点击查验
			if (this.nextBtn) {
				// 校验
				if (!this.validation()) return false;

				var invoiceDate = this.invoiceDate,
					param = {
					incomeParam: {
						invoiceCode: this.invoiceCode, 
						invoiceNum: this.invoiceNum,
						noTaxAmount: this.hasData && !this.hasCheckCode ? parseFloat(this.noTaxAmount).toFixed(2) : '',
						checkCode: this.checkCode,
						invoiceDate: invoiceDate.slice(0, 4) + invoiceDate.slice(5, 7) + invoiceDate.slice(8, 10),

						// 冗余字段
						taxNum: '',
						companyName: ''
					}
				};

				var invoiceType =  this.checkInvoiceType(this.invoiceCode);
				if (invoiceType === 'TYPE_ERROR') {
					this.$vux.toast.show({
						type: 'text',
						width: '238px',
						text: '发票类型错误'
					});
					return false;
				}

				this.$vux.loading.show({text: '正在查验'});

				
					url = '/app/check/allow/fpcy.do';
				
					
			
				// 参数aes加密
				param = this.AESencrypt(param);
				console.log(param)
				
				axios.post(ajaxDomain + url, qs.stringify({param: param}))
				.then((res) => {

					var data = this.AESdecrypt(res.data);
					var code = data.code;
					console.log(data)

					if (code== "1000") {
						this.$vux.loading.hide();

						// 成功 code: 1000
						this.$router.push({
							path: routerDomain + '/success'
						})

						store.set('invoice', data.invoice)
					} else {
						url = '/app/check/allow/irsfpcy.do';

						axios.post(ajaxDomain + url, qs.stringify({param: param}))
						.then((res) => {
							this.$vux.loading.hide();

							var data = this.AESdecrypt(res.data);
							var code = data.code;

							if (this.hasData && code== "0") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', '发票查验失败')
							} else if (code== "1000") {
								// 成功 code: 1000
								this.$router.push({
									path: routerDomain + '/success'
								})

								store.set('invoice', data.invoice)
							} else if (this.hasData && code== "1001") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', '查询税号不一致')
							} else if (code== "1002" || code== "3003" ) {

								// 本地数据库查询不到数据，改成国税数据库查找，调用第二个接口 /icenter/irsfpcy.do
								if (this.hasData) {
									// 国税数据库也查不到，发票不存在
									this.$router.push({
										path: routerDomain + '/fail'
									})
									store.set('msg', '查询不到数据')
								} else {
									this.$vux.toast.show({
										type: 'text',
										width: '238px',
										text: '请输入完整发票信息'
									});
								}

								this.hasData = true;
								if (invoiceType === 'TYPE_SPECIAL') {
									this.hasNoTaxAmountOne = true;
								} else if (invoiceType === 'TYPE_COMMON' || invoiceType === 'TYPE_NET_COMMON' || invoiceType === 'TYPE_ROLLUP') {
									this.hasCheckCode = true;
								} else if (invoiceType === 'TYPE_VEHICLE') {
									this.hasNoTaxAmountTwo = true;
								}
							} else if (this.hasData && code== "1003" || code== "5002" ) {
								// this.$vux.toast.show({
								// 	type: 'text',
								// 	width: '238px',
								// 	text: '查询无此票'
								// });

								// 国税数据库也查不到，发票不存在
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', '查询无此票')
							} else if (this.hasData && code == "1004") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', '超过该张发票当日查验次数（请于次日在次查验）！')
							} else if(this.hasData && code == "1005") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', '发票代码错误')
							} else if(this.hasData && code == "1006") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', '发票号码错误')
							} else if(this.hasData && code == "1007") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', '发票查验结果不一致')
							} else if (this.hasData && code == "2001") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', 'AES解密错误')
							} else if (this.hasData && code == "2002") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', 'AES加密错误')
							} else if (this.hasData && code == "3001") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', 'hbase入库错误')
							} else if (this.hasData && code == "4001") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', 'json解析错误')
							} else if (this.hasData && code == "5001") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', '国税接口登录令牌失败')
							} else if (this.hasData && code == "5002") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', '国税接口D1L查询发票信息失败')
							} else if (this.hasData && code == "5003") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', '网络服务请求失败')
							} else if (this.hasData && code == "6001") {
								this.$router.push({
									path: routerDomain + '/fail'
								})
								store.set('msg', '错误代码6001')
							} else if (this.hasData) {
								this.$vux.toast.show({
									type: 'text',
									width: '238px',
									text: '未知异常'
								});
							} else if (!this.hasData) {
								this.$vux.toast.show({
									type: 'text',
									width: '238px',
									text: '请输入完整发票信息'
								});

								this.hasData = true;
								if (invoiceType === 'TYPE_SPECIAL') {
									this.hasNoTaxAmountOne = true;
								} else if (invoiceType === 'TYPE_COMMON' || invoiceType === 'TYPE_NET_COMMON' || invoiceType === 'TYPE_ROLLUP') {
									this.hasCheckCode = true;
								} else if (invoiceType === 'TYPE_VEHICLE') {
									this.hasNoTaxAmountTwo = true;
								}
							}
						})
						.catch(err => {
							this.$vux.loading.hide();
							console.log(err)
						});

					}

					
				})
				.catch(err => {
					this.$vux.loading.hide();
					console.log(err)
				});
			}
		},
		// --method--
		// 确认查验
		check () {
			var url;
			// 点击查验
			if (this.nextBtn) {
				// 校验
				if (!this.validation()) return false;

				var invoiceDate = this.invoiceDate,
					param = {
					incomeParam: {
						invoiceCode: this.invoiceCode,
						invoiceNum: this.invoiceNum,
						noTaxAmount: this.hasData && !this.hasCheckCode ? parseFloat(this.noTaxAmount).toFixed(2) : '',
						
						checkCode: this.checkCode,
						invoiceDate: invoiceDate.slice(0, 4) + invoiceDate.slice(5, 7) + invoiceDate.slice(8, 10),

						// 冗余字段
						taxNum: '',
						companyName: ''
					}
				};
				
				var invoiceType =  this.checkInvoiceType(this.invoiceCode);
				if (invoiceType === 'TYPE_ERROR') {
					this.$vux.toast.show({
						type: 'text',
						width: '238px',
						text: '发票类型错误'
					});
					return false;
				}

				this.$vux.loading.show({text: '正在查验'});

				if (!this.hasData) {
					url = '/app/check/allow/fpcy.do';
				} else {
					url = '/app/check/allow/irsfpcy.do';
				}
				
				// 参数aes加密
				param = this.AESencrypt(param);
				console.log(param)
				console.log(qs.stringify({param: param}));
				
				axios.post(ajaxDomain + url, qs.stringify({param: param}))
				.then((res) => {
					this.$vux.loading.hide();

					var data = this.AESdecrypt(res.data);
					var code = data.code;
					console.log(data)
					if (this.hasData && code== "0") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', '发票查验失败')
					} else if (code== "1000") {
						// 成功 code: 1000
						this.$router.push({
							path: routerDomain + '/success'
						})

						store.set('invoice', data.invoice)
					} else if (this.hasData && code== "1001") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', '查询税号不一致')
					} else if (code== "1002" || code== "3003" ) {

						// 本地数据库查询不到数据，改成国税数据库查找，调用第二个接口 /icenter/irsfpcy.do
						if (this.hasData) {
							// 国税数据库也查不到，发票不存在
							this.$router.push({
								path: routerDomain + '/fail'
							})
							store.set('msg', '查询不到数据')
						} else {
							this.$vux.toast.show({
								type: 'text',
								width: '238px',
								text: '请输入完整发票信息'
							});
						}

						this.hasData = true;
						if (invoiceType === 'TYPE_SPECIAL') {
							this.hasNoTaxAmountOne = true;
						} else if (invoiceType === 'TYPE_COMMON' || invoiceType === 'TYPE_NET_COMMON' || invoiceType === 'TYPE_ROLLUP') {
							this.hasCheckCode = true;
						} else if (invoiceType === 'TYPE_VEHICLE') {
							this.hasNoTaxAmountTwo = true;
						}
					} else if (this.hasData && code== "1003" || code== "5002" ) {
						// this.$vux.toast.show({
						// 	type: 'text',
						// 	width: '238px',
						// 	text: '查询无此票'
						// });

						// 国税数据库也查不到，发票不存在
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', '查询无此票')
					} else if (this.hasData && code == "1004") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', '超过该张发票当日查验次数（请于次日在次查验）！')
					} else if(this.hasData && code == "1005") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', '发票代码错误')
					} else if(this.hasData && code == "1006") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', '发票号码错误')
					} else if(this.hasData && code == "1007") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', '发票查验结果不一致')
					} else if (this.hasData && code == "2001") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', 'AES解密错误')
					} else if (this.hasData && code == "2002") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', 'AES加密错误')
					} else if (this.hasData && code == "3001") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', 'hbase入库错误')
					} else if (this.hasData && code == "4001") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', 'json解析错误')
					} else if (this.hasData && code == "5001") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', '国税接口登录令牌失败')
					} else if (this.hasData && code == "5002") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', '国税接口D1L查询发票信息失败')
					} else if (this.hasData && code == "5003") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', '网络服务请求失败')
					} else if (this.hasData && code == "6001") {
						this.$router.push({
							path: routerDomain + '/fail'
						})
						store.set('msg', '错误代码6001')
					} else if (this.hasData) {
						this.$vux.toast.show({
							type: 'text',
							width: '238px',
							text: '未知异常'
						});
					} else if (!this.hasData) {
						this.$vux.toast.show({
							type: 'text',
							width: '238px',
							text: '请输入完整发票信息'
						});

						this.hasData = true;
						if (invoiceType === 'TYPE_SPECIAL') {
							this.hasNoTaxAmountOne = true;
						} else if (invoiceType === 'TYPE_COMMON' || invoiceType === 'TYPE_NET_COMMON' || invoiceType === 'TYPE_ROLLUP') {
							this.hasCheckCode = true;
						} else if (invoiceType === 'TYPE_VEHICLE') {
							this.hasNoTaxAmountTwo = true;
						}
					}
				})
				.catch(err => {
					this.$vux.loading.hide();
					console.log('err', err)
				});

			}
		},
		getParam(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
			var r = window.location.search.substr(1).match(reg); 
			if (r != null) return unescape(r[2]); 
			return null; 
		}
	},

}
export default Index;
</script>

<style>
	.fz-12 {
		font-size: 12px;
	}
	.fz-13 {
		font-size: 13px;
	}
	.fz-14 {
		font-size: 14px;
	}
	.fz-15 {
		font-size: 15px;
	}
	.fz-16 {
		font-size: 16px;
	}
	.text-center {
		text-align: center;
	}
	.text-orange {
		color: #B37745;
	}

	.headbar {
		position: absolute;
		top: -20px;
		left: 0;
		right: 0;
		height: 20px;
		background: #fff;
		z-index: 9999;
	}
	.vux-header.title {
		background-color: #FFF;
	}
	.vux-header.title .vux-header-title {
		color: #2C2C2C;
	}
	.vux-header .vux-header-left div.left-arrow:before {
		border: 1px solid #848589;
		border-width: 1px 0 0 1px;
	}
	.icon {
		display: inline-block;
		width: 21px;
		height: 21px;
	}
	.icon-scan {
		background: url('../assets/images/scan.png') center center no-repeat;
	}
	.icon-hand {
		background: url('../assets/images/scan.png') center center no-repeat;
	}
	.tips {
		padding: 15px 10px 0;
		color: #969696;
	}
	button.btn-check,
	a.btn-check {
		color: #FFF;
		background-color: #55A8EC;
	}
	.weui-btn.weui-btn_disabled:after {
		border: none;
	}

	/* 自定义表单 */
	.flex-title-wrap {
		position: relative;
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		padding: 10px 15px;
	}
	.flex-title-wrap:before {
		content: " ";
		position: absolute;
		left: 15px;
		right: 0;
		top: 0;
		border-top: 1px solid #D9D9D9;
		color: #D9D9D9;
		-webkit-transform-origin: 0 0;
		transform-origin: 0 0;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
	}
	.flex-title-wrap:after {
		content: " ";
		display: inline-block;
		height: 6px;
		width: 6px;
		border-width: 2px 2px 0 0;
		border-color: #C8C8CD;
		border-style: solid;
		-webkit-transform: matrix(0.71, 0.71, -0.71, 0.71, 0, 0);
		transform: matrix(0.71, 0.71, -0.71, 0.71, 0, 0);
		position: relative;
		top: -2px;
		position: absolute;
		top: 50%;
		margin-top: -4px;
		right: 12px;
	}
	.flex-title-wrap .flex-label label {
		display:block;
		width: 110px;
	}
	.flex-title-wrap .flex-value input {
		width: 100%;
		border: 0;
		outline: 0;
		-webkit-appearance: none;
		background-color: transparent;
		color: inherit;
		height: 1.41176471em;
		line-height: 1.41176471;
		font-size: inherit;
	}

	/* 消除vux按钮默认样式，包括disabled 和active时的样式 */
	button.btn-reset,
	a.btn-reset {
		color: #7A7A7A;
		background-color: #FFF;
	}
	/* 消除vux的input框的focus样式 */
	.calender-input,
	.vux-x-input .weui-input,
	.vux-x-input .weui-input:active,
	.vux-x-input .weui-input:focus {
		outline: none;
		background-color: transparent;
		-webkit-tap-highlight-color: transparent;
	}
	/* 消除日历点击的focus样式 */
	.index,
	.weui-cell .weui-cell__ft,
	.calender-wrap .calender-list table td,
	.calender-wrap .calender-list table td:focus,
	.calender-wrap .calender-list table td:active {
		background-color: transparent;
		-webkit-tap-highlight-color: transparent;
	}
	button.btn-check:not(.weui-btn_disabled):active,
	a.btn-check:not(.weui-btn_disabled):active {
		color: rgba(255, 255, 255, 0.6);
		background-color: #198FE9;
	}
	.btn-check.weui-btn_disabled.weui-btn_default {
		color: rgba(255, 255, 255, 0.6);
		background-color: #90C7F3;
	}
	button.btn-reset:not(.weui-btn_disabled):active,
	a.btn-reset:not(.weui-btn_disabled):active {
		color: rgba(0, 0, 0, 0.4);
		background-color: #DDE1E4;
	}
	.btn-reset.weui-btn_disabled.weui-btn_default {
		color: #C3C3C3;
		background-color: #DDE1E4;
	}
	/* 日期弹出框tab */
	.popup-tab {
		position: fixed;
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		top: 0;
		margin-bottom: 2px;
		width: 100%;
		height: 46px;
		line-height: 46px;
		justify-content: space-between;
		background-color: #FFF;
		z-index: inherit;
		/* overflow-y: hidden; */
	}
	
	.popup-tab:after {
		content: " ";
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		height: 1px;
		background-color: #F0F0F0;
	}
	.popup-tab a {
		display: block;
		padding: 0 12px;
		line-height: 46px;
		font-size: 16px;
	}
	/* 重绘calender日历 */
	.calender-wrap {
		margin-top: 46px;
		/* overflow-y: scroll;
    	-webkit-overflow-scrolling: touch; */
	}
	.calender-header {
		color: #030303;
		background-color: #F0F0F0;
		line-height: 30px;
		font-size: 14px;
		text-align: center;
	}
	.inline-calendar tr td.current > span.vux-calendar-each-date {
		background-color: #55A8EC;
	}
	.inline-calendar tr td > span.vux-calendar-each-date {
		width: 40px;
		height: 40px;
		line-height: 40px;
	}
	.lead-shadow {
		position: absolute;
		top: 0;
		left: 0;
		margin-top: 46px;
		width: 100%;
		height: 100%;
		/* background: rgba(0, 0, 0, 0.7); */
		z-index: 99;
	}
	.lead-shadow img {
		width: 100%;
		height: 100%;
		background: transparent;
		-webkit-tap-highlight-color: transparent;
	}
	.lead-shadow .lead-img-1 {
		position: absolute;
		top: 0;
		right: 20px;
	}
	.lead-shadow .lead-img-2 {
		position: absolute;
		top: 130px;
		left: 50%;
		margin-left: -92px;
	}
	.lead-shadow .lead-img-3 {
		position: absolute;
		top: 180px;
		left: 50%;
		margin-left: -68px;
	}

	[contenteditable = "true"], input, textarea {
		-webkit-user-select: auto!important;
		-khtml-user-select: auto!important;
		-moz-user-select: auto!important;
		-ms-user-select: auto!important;
		-o-user-select: auto!important;
		user-select: auto!important;
	}
</style>
